using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Threading;

namespace Tool.BaseClass
{
	public class GClass40
	{
		private AutoResetEvent autoResetEvent;

		public bool method_0(string string_0, string string_1, int int_0, string string_2)
		{
			this.autoResetEvent = new AutoResetEvent(false);
			Thread thread = new Thread(new ParameterizedThreadStart(this.method_1));
			thread.Start(new GClass41
			{
				Url = string_0,
				Path = string_1,
				TimeOut = int_0,
				LoadNumber = 0,
				UserAgent = string_2
			});
			this.autoResetEvent.WaitOne();
			return File.Exists(string_1);
		}

		private void method_1(object object_0)
		{
			GClass41 gClass = (GClass41)object_0;
			if (!File.Exists(gClass.Path))
			{
				Stream stream = null;
				FileStream fileStream = null;
				System.Net.HttpWebRequest httpWebRequest = null;
				System.Net.HttpWebResponse httpWebResponse = null;
				try
				{
					string text = GClass0.smethod_5(gClass.Url);
					httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(text);
					httpWebRequest.CookieContainer = new System.Net.CookieContainer();
					httpWebRequest.Headers.Add("Accept-Language", "zh-CN");
					if (!string.IsNullOrEmpty(gClass.UserAgent))
					{
						httpWebRequest.UserAgent = gClass.UserAgent;
					}
					GClass0.smethod_7(httpWebRequest, text);
					httpWebRequest.Timeout = gClass.TimeOut;
					httpWebRequest.ReadWriteTimeout = gClass.TimeOut;
					httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
					if (httpWebResponse == null || httpWebResponse.StatusCode != System.Net.HttpStatusCode.OK)
					{
						gClass.LoadNumber++;
						if (gClass.LoadNumber < 3)
						{
							this.method_1(object_0);
						}
					}
					else
					{
						stream = httpWebResponse.GetResponseStream();
						MemoryStream memoryStream = new MemoryStream();
						byte[] array = new byte[102400];
						if (!(httpWebResponse.ContentEncoding.ToLower() == "gzip"))
						{
							if (!(httpWebResponse.ContentEncoding.ToLower() == "deflate"))
							{
								int count;
								while ((count = stream.Read(array, 0, array.Length)) > 0)
								{
									memoryStream.Write(array, 0, count);
								}
								goto IL_1D5;
							}
							using (System.IO.Compression.DeflateStream deflateStream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress))
							{
								int count;
								while ((count = deflateStream.Read(array, 0, array.Length)) > 0)
								{
									memoryStream.Write(array, 0, count);
								}
								goto IL_1D5;
							}
						}
						using (System.IO.Compression.GZipStream gZipStream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress))
						{
							int count;
							while ((count = gZipStream.Read(array, 0, array.Length)) > 0)
							{
								memoryStream.Write(array, 0, count);
							}
						}
						IL_1D5:
						fileStream = new FileStream(gClass.Path, FileMode.Create);
						memoryStream.WriteTo(fileStream);
						memoryStream = null;
					}
				}
				catch
				{
					if (File.Exists(gClass.Path))
					{
						try
						{
							File.Delete(gClass.Path);
						}
						catch
						{
						}
					}
				}
				finally
				{
					if (fileStream != null)
					{
						fileStream.Close();
						fileStream.Dispose();
					}
					if (stream != null)
					{
						stream.Close();
						stream.Dispose();
					}
					if (httpWebResponse != null)
					{
						httpWebResponse.Close();
					}
					if (httpWebRequest != null)
					{
						httpWebRequest.Abort();
					}
				}
			}
			this.autoResetEvent.Set();
		}
	}
}
